package com.itheima.service.impl;

import com.itheima.common.constant.RedisConstant;
import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.utils.SMSUtils;
import com.itheima.common.utils.ValidateCodeUtils;
import com.itheima.service.ValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {
    @Autowired
    private RedisTemplate  redisTemplate;

    /**
     * 体检预约发送验证码
     * @param telephone
     * @return
     */
    @Override
    public String sendMessageCode(String telephone) {
        String code = ValidateCodeUtils.generateValidateCode(4).toString();//生成验证码
        SMSUtils.sendMessage(SMSUtils.ORDER_NOTICE,telephone,code);

        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(telephone,code); //redis存入key
        redisTemplate.expire(telephone,3,TimeUnit.MINUTES); //设置过期时间

        //设置过期时间
        return code;
    }
}
